package com.jjmmbb.anydolist;

import java.util.ArrayList;

import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.jjmmbb.db.adapter.ManageAdapter;
import com.jjmmbb.db.pojo.Item;
import com.jjmmbb.dolist.ex.HKDialog;

public class ManageActivity extends BaseActivity implements
		OnItemClickListener, OnClickListener, OnItemLongClickListener {
	ListView listView;
	ManageAdapter adapter;
	Button btFirst, btDoing, btFinish, btDelete, btSelectAll, btSelectInverse;

	// Gallery buttonGallery;
	// MenuAdatper buttonsAdapter;
	ArrayList<Item> list;

	int[] buttonArray = { R.string.manage_doing, R.string.manage_finish,
			R.string.manage_first, R.string.manage_delete,
			R.string.menu_select_all };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manage);

		btFirst = (Button) findViewById(R.id.manage_first);
		btDoing = (Button) findViewById(R.id.manage_doing);
		btFinish = (Button) findViewById(R.id.manage_finish);
		btDelete = (Button) findViewById(R.id.manage_delete);
		btSelectAll = (Button) findViewById(R.id.manage_select_all);
		btSelectInverse = (Button) findViewById(R.id.manage_select_inverse);

		listView = (ListView) findViewById(R.id.manage_list);
		listView.setOnItemClickListener(this);
		listView.setOnItemLongClickListener(this);

		btFirst.setOnClickListener(this);
		btDoing.setOnClickListener(this);
		btFinish.setOnClickListener(this);
		btDelete.setOnClickListener(this);
		btSelectAll.setOnClickListener(this);
		btSelectInverse.setOnClickListener(this);

		ini();
	}

	void ini() {
		// buttonGallery = (Gallery) findViewById(R.id.manage_buttons);
		// buttonGallery.setOnItemClickListener(new GalleryItemClickListener());

		// buttonsAdapter = new MenuAdatper(this, buttonArray);
		// buttonGallery.setAdapter(buttonsAdapter);
		// buttonGallery.setSelection(2);
	}

	/**
	 * 重新加载列表
	 */
	void updateList() {
		list = ap.c.itemManager.getListOrder();
		adapter = new ManageAdapter(this, list);
		listView.setAdapter(adapter);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (null == adapter) {
			updateList();
		} else {
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.manage_first:
			String time = Util.getFormateTime();
			for (Item item : list) {
				if (item.isSelect) {
					item.time = time;
					ap.c.itemManager.update(item);
				}
			}
			updateList();
			break;
		case R.id.manage_doing:
			for (Item item : list) {
				if (item.isSelect) {
					item.type = 0;
					ap.c.itemManager.update(item);
				}
			}
			updateList();
			break;
		case R.id.manage_finish:
			for (Item item : list) {
				if (item.isSelect) {
					item.type = 1;
					ap.c.itemManager.update(item);
				}
			}
			updateList();
			break;
		case R.id.manage_delete:
			HKDialog dialog = new HKDialog(this,
					res.getString(R.string.manage_delete_sure)) {
				@Override
				protected void bt1Click() {
					delete();
					super.bt1Click();
				}
			};
			dialog.show();
			dialog.setButtonVisible(2, true);
			break;
		case R.id.manage_select_all:
			for (Item temp : list) {
				temp.isSelect = true;
			}
			adapter.notifyDataSetChanged();
			break;
		case R.id.manage_select_inverse:
			for (Item temp : list) {
				temp.isSelect = !temp.isSelect;
			}
			adapter.notifyDataSetChanged();
			break;
		}
	}

	private void delete() {
		for (Item item : list) {
			if (item.isSelect) {
				item.type = 2;
				ap.c.itemManager.delete(item.id);
			}
		}
		updateList();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Item item = list.get(position);
		TextView tv = (TextView) view;
		if (item.isSelect) {
			tv.setTextColor(Color.BLACK);
			item.isSelect = false;
		} else {
			tv.setTextColor(Color.YELLOW);
			item.isSelect = true;
		}
	}

	// ---------------弹出菜单-------------------------//
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, R.id.oMenu_selectAll, 0, R.string.menu_select_all).setIcon(
				R.drawable.icon_finish);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.oMenu_selectAll:
			for (Item temp : list) {
				temp.isSelect = true;
			}
			adapter.notifyDataSetChanged();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		onAutoLink(position);
		return false;
	}

	// 跳转编辑界面
	// private void gotoItemEditActivity() {
	// Bundle bundle = new Bundle();
	// bundle.putString("goManage", "manage");
	// Intent intent = new Intent();
	// intent.setClass(this, ItemEditActivity.class);
	// intent.putExtras(bundle);
	// startActivity(intent);
	// }

	private void onAutoLink(int position) {
		HKDialog dialog = new HKDialog(this, getStr(R.string.dialog_title),
				list.get(position).title);
		dialog.show();
	}

}
